package com.fqgj.sentry.controller.mc;


import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.biz.mc.ComboBoxBusiness;
import com.fqgj.sentry.biz.mc.UserBusiness;
import com.fqgj.sentry.biz.mc.vo.application.ApplicationVo;
import com.fqgj.sentry.biz.mc.vo.role.RoleVo;
import com.fqgj.sentry.biz.mc.domain.AddUserInfo;
import com.fqgj.sentry.common.annotations.ParamsValidate;
import com.fqgj.sentry.common.cache.RequestLocalInfo;
import com.fqgj.sentry.common.response.ApiOutput;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.controller.mc.request.AddGroupManagerRequest;
import com.fqgj.sentry.controller.mc.request.AddUserRequest;
import com.fqgj.sentry.biz.mc.vo.user.GroupManagerVo;
import com.fqgj.sentry.controller.mc.request.ModifyGroupManagerVo;
import com.fqgj.sentry.controller.mc.request.UserPageVo;
import com.fqgj.sentry.manage.enums.UserLevelEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-18 上午10:31
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserBusiness userBusiness;
    @Autowired
    private ComboBoxBusiness comboBoxBusiness;

    @ParamsValidate
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiOutput queryUserListForPage(@RequestBody UserPageVo userPageVo) {
        ApiPage apiPage = userBusiness.queryUserListForPage(userPageVo.getAccount(), RequestLocalInfo.getCurrentMerchantId(), userPageVo.getPageNum(), userPageVo.getPageSize());
        return new ApiOutput<>(apiPage);
    }


    @RequestMapping(value = "/list/group/manager", method = RequestMethod.POST)
    public ApiOutput queryGroupManager(@RequestParam Long userId) {
        List<GroupManagerVo> groupManagerVoList = userBusiness.queryGroupManagerByUserId(userId);
        return new ApiOutput<>("groupManagerList", groupManagerVoList);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiOutput addUser(@RequestBody AddUserRequest addUserRequest) {
        AddUserInfo addUserInfo = new AddUserInfo();
        addUserInfo.setName(addUserRequest.getName())
                .setMobile(addUserRequest.getMobile())
                .setLevel(UserLevelEnums.nomarl_user.getLevel())
                .setAccount(addUserRequest.getAccount())
                .setMerchantId(RequestLocalInfo.getCurrentMerchantId());
        Long userId = userBusiness.addUser(addUserInfo);
        if (CollectionUtils.isNotEmpty(addUserRequest.getApplicationId())
                && CollectionUtils.isNotEmpty(addUserRequest.getRoleId())) {
            for (String appId : addUserRequest.getApplicationId()) {
                userBusiness.addGroupManager(RequestLocalInfo.getCurrentMerchantId(), userId, addUserRequest.getRoleId(), appId);
            }
        }
        return new ApiOutput<>();
    }

    @ParamsValidate
    @RequestMapping(value = "/add/group/manager", method = RequestMethod.POST)
    public ApiOutput addGroupManager(@RequestBody AddGroupManagerRequest groupManagerRequest) {
        for (String appId : groupManagerRequest.getApplicationId()) {
            userBusiness.addGroupManager(RequestLocalInfo.getCurrentMerchantId(), groupManagerRequest.getUserId(), groupManagerRequest.getRoleId(), appId);
        }
        return new ApiOutput<>();
    }


    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ApiOutput delUser(@RequestParam Long userId) {
        userBusiness.delUser(userId, RequestLocalInfo.getCurrentMerchantId());
        return new ApiOutput<>();
    }

    @RequestMapping(value = "/del/group/manager", method = RequestMethod.POST)
    public ApiOutput delGroupManager(@RequestParam Long groupManagerId) {
        userBusiness.delGroupManager(groupManagerId);
        return new ApiOutput<>();
    }

    @ParamsValidate
    @RequestMapping(value = "/modify/group/manager", method = RequestMethod.POST)
    public ApiOutput modifyGroupManager(@RequestBody ModifyGroupManagerVo modifyGroupManagerVo) {
        userBusiness.modifyGroupManger(modifyGroupManagerVo.getGroupManagerId(), modifyGroupManagerVo.getRoleId());
        return new ApiOutput<>();
    }

    @RequestMapping(value = "/reset/password", method = RequestMethod.POST)
    public ApiOutput resetPassword(@RequestParam Long userId) {
        userBusiness.resetPassword(userId, RequestLocalInfo.getCurrentMerchantId());
        return new ApiOutput<>();
    }

    @RequestMapping(value = "/role/combo", method = RequestMethod.POST)
    public ApiOutput getRoleComboBox() {
        List<RoleVo> roleVoList = comboBoxBusiness.queryRoleComboBox(RequestLocalInfo.getCurrentMerchantId());
        return new ApiOutput(roleVoList);
    }

    @RequestMapping(value = "/app/combo", method = RequestMethod.POST)
    public ApiOutput getAppComboBox() {
        List<ApplicationVo> applicationVoList = comboBoxBusiness.queryAppComboBox(RequestLocalInfo.getCurrentMerchantId());
        return new ApiOutput(applicationVoList);
    }
}
